//
//  GXDPDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/24.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "GXDPDetViewController.h"

#import "GXDPView.h"
#import "GXDPDetCell.h"
#import "Masonry.h"
#import "PinJiaCell.h"
#import "PingJiaViewController.h"

#import "GXDPPayViewController.h"
#import "GXShopView.h"
@interface GXDPDetViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UILabel *LabLine;
    NSMutableArray *arr1;//菜单栏数据
    GXShopView *shopView;
    NSMutableArray *array;//数据源
    NSMutableDictionary *dics;//评论数
    NSMutableArray *plArray;
}

@property (nonatomic,strong)UITableView *tableView1;
@property (nonatomic,strong)UITableView *tableView2;
@property (nonatomic,strong)UITableView *tableView3;

@end

@implementation GXDPDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"店铺详情"];
    array = [NSMutableArray array];
    arr1 = [NSMutableArray array];
    dics = [NSMutableDictionary dictionary];
    plArray = [NSMutableArray array];
    
    [self loadLeftData];
    [self loadData2];
    [self loadUI];
}

- (void)loadUI{
    UIScrollView *sclView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-60)];
    sclView.contentSize = CGSizeMake(ScreenWidth, 760);
    sclView.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self.view addSubview:sclView];
    
    //购物车视图
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-60, ScreenWidth, 60.0)];
    footerView.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self.view addSubview:footerView];
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ShowShop)];
    [footerView addGestureRecognizer:tap];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth/3*2, 0, ScreenWidth/3, 60.0);
    btn.backgroundColor = COLOR_RGB(102, 102, 102, 1);
    [btn setTitle:@"未选购" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickPay) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:btn];
    
    UIView *viewL = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 50, 50)];
    viewL.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [footerView addSubview:viewL];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"购物车"]];
    [viewL addSubview:imageView];
    [imageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(viewL.centerX);
        make.centerY.equalTo(viewL.centerY);
    }];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.textColor = COLOR_RGB(153, 153, 153, 1);
    lab.text = @"￥0.00";
    lab.font = [UIFont systemFontOfSize:18.0];
    [footerView addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewL.right).offset(8);
        make.top.equalTo(footerView.top).offset(8);
    }];
    
    UILabel *lab2 = [[UILabel alloc] init];
    lab2.textColor = COLOR_RGB(128, 128, 128, 1);
    lab2.text = @"另需6.0元配送费";
    lab2.font = [UIFont systemFontOfSize:12.0];
    [footerView addSubview:lab2];
    [lab2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewL.right).offset(8);
        make.top.equalTo(lab.bottom).offset(0);
    }];
    
    //头视图
    GXDPView *view = [[[NSBundle mainBundle] loadNibNamed:@"GXDPView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 280);
    [sclView addSubview:view];
    
    view.labName.text = _name;
    view.labdet.text = _det;
    view.labPlace.text = [NSString stringWithFormat:@"地址：%@",_place];
    view.labOrder.text = _order;
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, view.frame.origin.y+view.frame.size.height+10, ScreenWidth, 40.0)];
    headView.backgroundColor = [UIColor whiteColor];
    [sclView addSubview:headView];
    
    UILabel *labLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
    labLine.backgroundColor = [UIColor grayColor];
    [headView addSubview:labLine];
    //商品
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(0, 0, 65, 40);
    [btn1 setTitle:@"商品" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [btn1 addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:btn1];
    //评价
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(65, 0, 100, 40);
    
    NSString *pl = [NSString stringWithFormat:@"评价(%li)",plArray.count];
    [btn2 setTitle:pl forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [btn2 addTarget:self action:@selector(click2) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:btn2];
    
    LabLine = [[UILabel alloc] initWithFrame:CGRectMake(16, 36, 30, 4)];
    LabLine.backgroundColor = COLOR_RGB(65, 132, 255, 1);
    [headView addSubview:LabLine];
    //280+40+440
    _tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, headView.frame.origin.y+headView.frame.size.height, 90, 440) style:UITableViewStylePlain];
    _tableView1.delegate = self;
    _tableView1.dataSource = self;
    _tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
    [sclView addSubview:_tableView1];
    
    _tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(90, headView.frame.origin.y+headView.frame.size.height, ScreenWidth-90, 440) style:UITableViewStylePlain];
    _tableView2.delegate = self;
    _tableView2.dataSource = self;
    [sclView addSubview:_tableView2];
    
    _tableView3 = [[UITableView alloc] initWithFrame:CGRectMake(0, headView.frame.origin.y+headView.frame.size.height, ScreenWidth, 440) style:UITableViewStylePlain];
    _tableView3.delegate = self;
    _tableView3.dataSource = self;
    _tableView3.hidden = YES;
    [sclView addSubview:_tableView3];
    
    
}
//
- (void)ShowShop{
    if (![shopView superview]) {
        shopView = [[GXShopView alloc] init];
        [self.view addSubview:shopView];
        [shopView show];
    }
    
}
//商品事件
- (void)click1{
    [UIView animateWithDuration:0.3 animations:^{
        LabLine.frame = CGRectMake(16, 36, 30, 4);
    }];
    _tableView1.hidden = NO;
    _tableView2.hidden = NO;
    _tableView3.hidden = YES;
}
//评论事件
- (void)click2{
    [UIView animateWithDuration:0.3 animations:^{
        LabLine.frame = CGRectMake(85, 36, 64, 4);
    }];
    _tableView1.hidden = YES;
    _tableView2.hidden = YES;
    _tableView3.hidden = NO;
}

- (void)clickPay{
    GXDPPayViewController *gxpay = [[GXDPPayViewController alloc] init];
    [self.navigationController pushViewController:gxpay animated:YES];
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _tableView1) {
        return arr1.count;
    }else if (tableView == _tableView2){
        return array.count;
    }
    return plArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _tableView1) {
        static NSString *cellId = @"Cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        cell.textLabel.font = [UIFont systemFontOfSize:14.0];
        cell.textLabel.textColor = COLOR_RGB(77, 77, 77, 1);
        NSDictionary *dic = arr1[indexPath.row];
        cell.textLabel.text = [dic objectForKey:@"name"];
        
        return cell;
    }else if (tableView == _tableView2){
        static NSString *cellId = @"Cell2";
        GXDPDetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"GXDPDetCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dic = array[indexPath.row];
        cell.labName.text = [dic objectForKey:@"name"];
        cell.labDet.text = [dic objectForKey:@"introduction"];
        NSString *price = [NSString stringWithFormat:@"￥ %.2f",[[dic objectForKey:@"price"] floatValue]];
        cell.labPrice.text = price;
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"img"]];
        [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TB2qzP8eCFjpuFjSspbXXXagVXa_!!2939259613_看图王.web"]];
        
        
        return cell;
    }else{
        static NSString *cellId = @"Cell3";
        PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (plArray.count == 0) {
            return cell;
        }
        
        NSDictionary *dic = plArray[indexPath.row];
        cell.labName.text = [dic objectForKey:@"nickname"];
        cell.labDate.text = [dic objectForKey:@"comment"];
        cell.labDate.text = [dic objectForKey:@"commenttime"];
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"avatar"]];
        [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TIM图片20170810135854"]];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _tableView1) {
        return 44.0;
    }else if (tableView == _tableView2){
        return 100.0;
    }else{
        return 100.0;
    }
}
//头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"店铺评价";
    lab.font = [UIFont systemFontOfSize:15.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.left).offset(16);
        make.centerY.equalTo(view.centerY);
    }];
    
    UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(16, 39, ScreenWidth-32, 0.5)];
    lab2.backgroundColor = [UIColor grayColor];
    [view addSubview:lab2];
    
    UIImageView *img1 = [[UIImageView alloc] init];
    img1.image = [UIImage imageNamed:@"星-2-拷贝"];
    [view addSubview:img1];
    [img1 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(view.right).offset(-16);
    }];
    
    UIImageView *img2 = [[UIImageView alloc] init];
    img2.image = [UIImage imageNamed:@"星-2-拷贝"];
    [view addSubview:img2];
    [img2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(img1.left);
    }];
    
    UIImageView *img3 = [[UIImageView alloc] init];
    img3.image = [UIImage imageNamed:@"星-2-拷贝"];
    [view addSubview:img3];
    [img3 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(img2.left);
    }];
    
    UIImageView *img4 = [[UIImageView alloc] init];
    img4.image = [UIImage imageNamed:@"星-2-拷贝"];
    [view addSubview:img4];
    [img4 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(img3.left);
    }];
    
    UIImageView *img5 = [[UIImageView alloc] init];
    img5.image = [UIImage imageNamed:@"星-2-拷贝"];
    [view addSubview:img5];
    [img5 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(img4.left);
    }];
    
    return view;
}
//头视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView == _tableView3) {
        return 40.0;
    }
    return 0;
}

//尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, ScreenWidth, 40.0);
    [btn setTitle:@"查看全部评价" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2+35, 14, 60, 12)];
    
    if (plArray.count == 0) {
        lab.text = @"(0)";
    }else{
        NSString *pl = [NSString stringWithFormat:@"(%@)",[[dics objectForKey:@"commentcount"] objectForKey:@"count"]];
        lab.text = pl;
    }
    
    lab.font = [UIFont systemFontOfSize:12.0];
    lab.textColor = [UIColor redColor];
    [view addSubview:lab];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (tableView == _tableView3) {
        return 40.0;
    }
    return 0;
}

- (void)click{
    PingJiaViewController *pinjia = [[PingJiaViewController alloc] init];
    [self.navigationController pushViewController:pinjia animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _tableView1) {
        NSString *classid = [arr1[indexPath.row] objectForKey:@"id"];
        [self loadRightData:classid];
        [_tableView2 reloadData];
        
    }else if (tableView == _tableView2){
        
    }else{
        
    }
    
}

//左侧数据
- (void)loadLeftData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Cleaning/classtype/merchantid/12",urlHead];
    [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"左侧res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            arr1 = [responseObject objectForKey:@"result"];
            NSString *classid = [arr1[0] objectForKey:@"id"];
            [self loadRightData:classid];
        }
        
        [_tableView1 reloadData];
        
        //默认选中0
        [_tableView1 selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionBottom];
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

//右侧店铺详情
- (void)loadRightData:(NSString *)classid{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:classid forKey:@"classid"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Cleaning/details",urlHead];
    
    [manager GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"店铺详情res:%@",responseObject);
        array = [responseObject objectForKey:@"result"];
        
        [_tableView2 reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

//干洗-评论
- (void)loadData2{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:@"" forKey:@""];
    [parmas setObject:@"" forKey:@""];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Merchantcomment/cleaning/merchantid/12",urlHead];
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"干洗-评论res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            dics = [responseObject objectForKey:@"result"];
            plArray = [dics objectForKey:@"comment"];
        }
        
        [_tableView3 reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
